//
//  ZGSQuestionController.m
//  MapleLeaf
//
//  Created by dllo on 15/11/26.
//  Copyright © 2015年 AntennaBabyTeam. All rights reserved.
//

#import "ZGSQuestionController.h"
#import "ZGSOneQuestionModel.h"
#import "ZGSMineViewController.h"
#import "ZGSOneArticleViewController.h"
#import "ZGSArticleController.h"
#import "ZGSArtleViewController.h"
@interface ZGSQuestionController ()
@property (nonatomic, retain) UIView *headBgView;
@property (nonatomic, retain) UIScrollView *scrollView;
@property (nonatomic, retain) ZGSOneQuestionModel *question;
@property (nonatomic, retain) MBProgressHUD *hud;

@end

@implementation ZGSQuestionController
#pragma mark - createData

- (void)createData {
    
    [self createHeadBgView];
    
    AFNetworkReachabilityManager *nManager = [AFNetworkReachabilityManager sharedManager];
    
    // 开始检测
    [nManager startMonitoring];
    [nManager setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
        if (status == 1 || status == 2) {
            NSDate *date = [NSDate date];
            NSDateFormatter *formatter = [[NSDateFormatter alloc] init] ;
            formatter.dateFormat = @"yyyy-MM-dd";
            NSString *strDate = [formatter stringFromDate:date];
            NSLog(@"%@", strDate);
            NSDictionary *postDic = @{@"strDate":date, @"strRow":@"1"};
            [BaseAFN POSTURLByString:@"http://rest.wufazhuce.com/OneForWeb/one/getQ_N" bodyByDic:postDic AFNData:^(id result) {
                NSDictionary *dic = result[@"questionAdEntity"];
                
                self.question = [[ZGSOneQuestionModel alloc] init];
                [self.question setValuesForKeysWithDictionary:dic];
                
                [self.hud removeFromSuperview];
                
                [self createHeadBgView];
                [self createViews];

                
            }];
        }
        else {
            [self.hud removeFromSuperview];
            
            MBProgressHUD *hud = [[MBProgressHUD alloc] initWithView:self.view];
            [self.view addSubview:hud];
            hud.mode = MBProgressHUDModeText;
            hud.labelText = @"数据请求失败,请检查网络后重试";
            [hud showAnimated:YES whileExecutingBlock:^{
                sleep(1);
            } completionBlock:^{
                [hud removeFromSuperview];
            }];
        }
        
    }];
    
    // 停止检测
    [nManager stopMonitoring];
    
}



#pragma mark - createViews

- (void)createHeadBgView {
    self.headBgView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, 64)];
    self.headBgView.backgroundColor = [UIColor colorWithRed:0.929f green:0.106f blue:0.149f alpha:1.00f];
    [self.view addSubview:self.headBgView];
    
    UIButton *backButton = [UIButton buttonWithType:UIButtonTypeSystem];
    backButton.frame = CGRectMake(10, 27, 35, 25);
    [backButton setTitle:@"返回" forState:UIControlStateNormal];
    backButton.tintColor = [UIColor whiteColor];
    backButton.titleLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:17];
    [backButton addTarget:self action:@selector(backAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.headBgView addSubview:backButton];
    
//    UIButton *shareButton = [UIButton buttonWithType:UIButtonTypeSystem];
//    shareButton.frame = CGRectMake(WIDTH - 40, 25, 25, 25);
//    [shareButton setBackgroundImage:[UIImage imageNamed:@"iconfont-share.png"] forState:UIControlStateNormal];
//    [shareButton addTarget:self action:@selector(shareAction:) forControlEvents:UIControlEventTouchUpInside];
//    [self.headBgView addSubview:shareButton];
    
    UIButton *sentenceButton = [UIButton buttonWithType:UIButtonTypeSystem];
    sentenceButton.frame = CGRectMake(55, 27, 50, 25);
    [sentenceButton setTitle:@"句子" forState:UIControlStateNormal];
    sentenceButton.tintColor = [UIColor whiteColor];
    sentenceButton.layer.cornerRadius = 13;
    sentenceButton.titleLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:16];
    [sentenceButton addTarget:self action:@selector(changePageAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.headBgView addSubview:sentenceButton];
    
    UIButton *acticleButton = [UIButton buttonWithType:UIButtonTypeSystem];
    acticleButton.frame = CGRectMake(110, 27, 50, 25);
    [acticleButton setTitle:@"文章" forState:UIControlStateNormal];
    acticleButton.tintColor = [UIColor whiteColor];
    acticleButton.titleLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:16];
    [acticleButton addTarget:self action:@selector(changePageAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.headBgView addSubview:acticleButton];
    
    UIButton *questionButton = [UIButton buttonWithType:UIButtonTypeSystem];
    questionButton.frame = CGRectMake(165, 27, 50, 25);
    [questionButton setTitle:@"问题" forState:UIControlStateNormal];
    questionButton.tintColor = [UIColor colorWithRed:0.929f green:0.106f blue:0.149f alpha:1.00f];
    questionButton.backgroundColor = [UIColor whiteColor];
    questionButton.layer.cornerRadius = 13;
    questionButton.titleLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:16];
    [self.headBgView addSubview:questionButton];
}



- (void)createViews {
    self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 64, WIDTH, HEIGHT - 64 - 49)];
    [self.view addSubview:self.scrollView];
    
    UILabel *dateLabel = [[UILabel alloc] initWithFrame:CGRectMake(15, 20, 150, 15)];
    dateLabel.font = [UIFont fontWithName:@"ArialUnicodeMS" size:15];
    NSString *dayStr = [self.question.strQuestionMarketTime substringFromIndex:self.question.strQuestionMarketTime.length - 2];
    
    NSString *monthStr = [self.question.strQuestionMarketTime substringWithRange:NSMakeRange(self.question.strQuestionMarketTime.length - 5, 2)];
    
    NSString *yearStr = [self.question.strQuestionMarketTime substringToIndex:4];
    
    NSInteger month = [monthStr integerValue];
    switch (month) {
        case 1:
            dateLabel.text = [NSString stringWithFormat:@"January %@,%@", dayStr, yearStr];
            break;
        case 2:
            dateLabel.text = [NSString stringWithFormat:@"February %@,%@", dayStr, yearStr];
            break;
        case 3:
            dateLabel.text = [NSString stringWithFormat:@"March %@,%@", dayStr, yearStr];
            break;
        case 4:
            dateLabel.text = [NSString stringWithFormat:@"April %@,%@", dayStr, yearStr];
            break;
        case 5:
            dateLabel.text = [NSString stringWithFormat:@"May %@,%@", dayStr, yearStr];
            break;
        case 6:
            dateLabel.text = [NSString stringWithFormat:@"June %@,%@", dayStr, yearStr];
            break;
        case 7:
            dateLabel.text = [NSString stringWithFormat:@"July %@,%@", dayStr, yearStr];
            break;
        case 8:
            dateLabel.text = [NSString stringWithFormat:@"August %@,%@", dayStr, yearStr];
            break;
        case 9:
            dateLabel.text = [NSString stringWithFormat:@"September %@,%@", dayStr, yearStr];
            break;
        case 10:
            dateLabel.text = [NSString stringWithFormat:@"October %@,%@", dayStr, yearStr];
            break;
        case 11:
            dateLabel.text = [NSString stringWithFormat:@"November %@,%@", dayStr, yearStr];
            break;
        case 12:
            dateLabel.text = [NSString stringWithFormat:@"December %@,%@", dayStr, yearStr];
            break;
            
        default:
            break;
    }
    [self.scrollView addSubview:dateLabel];
    
    UIImageView *questionImageView = [[UIImageView alloc] initWithFrame:CGRectMake(15, 70, 46, 46)];
    questionImageView.image = [UIImage imageNamed:@"iconfont-icon-wentizixun.png"];
    [self.scrollView addSubview:questionImageView];
    
    UILabel *questionLabel = [[UILabel alloc] initWithFrame:CGRectMake(80, 70, WIDTH - 100, 46)];
    questionLabel.numberOfLines = 0;
    questionLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:17];
    NSMutableAttributedString *qAttributedString = [[NSMutableAttributedString alloc]initWithString:self.question.strQuestionTitle];
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc]init];
    [paragraphStyle setLineSpacing:5];
    [qAttributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, self.question.strQuestionTitle.length)];
    questionLabel.attributedText = qAttributedString;
    [self.scrollView addSubview:questionLabel];
    
    UILabel *qContentLabel = [[UILabel alloc] initWithFrame:CGRectMake(15, 143, WIDTH - 30, 1000)];
    qContentLabel.numberOfLines = 0;
    qContentLabel.font = [UIFont systemFontOfSize:16];
    NSMutableAttributedString *qcAttributedString = [[NSMutableAttributedString alloc]initWithString:self.question.strQuestionContent];
    NSMutableParagraphStyle *qcParagraphStyle = [[NSMutableParagraphStyle alloc]init];
    [qcParagraphStyle setLineSpacing:10];
    [qcAttributedString addAttribute:NSParagraphStyleAttributeName value:qcParagraphStyle range:NSMakeRange(0, self.question.strQuestionContent.length)];
    qContentLabel.attributedText = qcAttributedString;
    [qContentLabel sizeToFit];
    [self.scrollView addSubview:qContentLabel];
    
    UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(10, 143 + qContentLabel.frame.size.height + 23, WIDTH - 20, 1)];
    lineView.layer.borderColor = [[UIColor colorWithRed:0.659f green:0.659f blue:0.659f alpha:1.00f] CGColor];
    lineView.layer.borderWidth = 1;
    [self.scrollView addSubview:lineView];
    
    UIImageView *askImageView = [[UIImageView alloc] initWithFrame:CGRectMake(15, 166 + qContentLabel.frame.size.height + 20, 46, 46   )];
    askImageView.image = [UIImage imageNamed:@"iconfont-svgmoban55.png"];
    [self.scrollView addSubview:askImageView];
    
    UILabel *answerLabel = [[UILabel alloc] initWithFrame:CGRectMake(80, 166 + qContentLabel.frame.size.height + 20, WIDTH - 100, 46)];
    answerLabel.numberOfLines = 0;
    answerLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:17];
    NSMutableAttributedString *aAttributedString = [[NSMutableAttributedString alloc]initWithString:self.question.strAnswerTitle];
    NSMutableParagraphStyle *aParagraphStyle = [[NSMutableParagraphStyle alloc]init];
    [paragraphStyle setLineSpacing:5];
    [aAttributedString addAttribute:NSParagraphStyleAttributeName value:aParagraphStyle range:NSMakeRange(0, self.question.strAnswerTitle.length)];
    answerLabel.attributedText = aAttributedString;
    [self.scrollView addSubview:answerLabel];
    
    UILabel *aContentLabel = [[UILabel alloc] initWithFrame:CGRectMake(15, 232 + qContentLabel.frame.size.height + 20, WIDTH - 30, 5000)];
    aContentLabel.numberOfLines = 0;
    aContentLabel.font = [UIFont systemFontOfSize:16];
    NSString *aContent = self.question.strAnswerContent;
    aContent = [aContent stringByReplacingOccurrencesOfString:@"<br>" withString:@"\n"];
    aContent = [aContent stringByReplacingOccurrencesOfString:@"<i>" withString:@""];
    aContent = [aContent stringByReplacingOccurrencesOfString:@"</i>" withString:@""];
    NSMutableAttributedString *acAttributedString = [[NSMutableAttributedString alloc]initWithString:aContent];
    NSMutableParagraphStyle *acParagraphStyle = [[NSMutableParagraphStyle alloc]init];
    [acParagraphStyle setLineSpacing:10];
    [acAttributedString addAttribute:NSParagraphStyleAttributeName value:acParagraphStyle range:NSMakeRange(0, aContent.length)];
    aContentLabel.attributedText = acAttributedString;
    [aContentLabel sizeToFit];
    [self.scrollView addSubview:aContentLabel];
    
    self.scrollView.contentSize = CGSizeMake(0, 252 + qContentLabel.frame.size.height + aContentLabel.frame.size.height + 20);
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    self.hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    [self createData];

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - ButtonAction

- (void)backAction:(UIButton *)button {
    
    CATransition *transition = [CATransition animation];
    transition.duration = 1.0f;
    transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    transition.type = @"cube";
    transition.subtype = kCATransitionFromLeft;
    transition.delegate = self;
    [self.navigationController.view.layer addAnimation:transition forKey:nil];
    
    ZGSMineViewController *userVC = [[ZGSMineViewController alloc] init];
    [self.navigationController pushViewController:userVC animated:YES];
}

// 换页
- (void)changePageAction:(UIButton *)button {
    if ([button.titleLabel.text isEqualToString:@"句子"]) {
        CATransition *transition = [CATransition animation];
        transition.duration = 1.0f;
        transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
        transition.type = @"pageCurl";
        transition.subtype = kCATransitionFromLeft;
        transition.delegate = self;
        [self.navigationController.view.layer addAnimation:transition forKey:nil];
        
        ZGSArtleViewController *osVC = [[ZGSArtleViewController alloc] init];
        [self.navigationController pushViewController:osVC animated:YES];
    }
    else if ([button.titleLabel.text isEqualToString:@"文章"]) {
        CATransition *transition = [CATransition animation];
        transition.duration = 1.0f;
        transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
        transition.type = @"pageCurl";
        transition.subtype = kCATransitionFromLeft;
        transition.delegate = self;
        [self.navigationController.view.layer addAnimation:transition forKey:nil];
        
        ZGSOneArticleViewController *oaVC = [[ZGSOneArticleViewController alloc] init];
        [self.navigationController pushViewController:oaVC animated:YES];
    }
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
